
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.app.ad.IAdPayOrderAppService;
import com.hlkj.pay.app.ad.IAdRefundOrderAppService;
import com.hlkj.pay.app.common.IUploadService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.vo.ad.req.AdOrderRefundApplyReq;
import com.hlkj.pay.vo.ad.req.AdSubmitOrderReq;
import com.hlkj.pay.vo.ad.resp.AdH5Resp;
import com.hlkj.pay.vo.ad.resp.AdPayOrderResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "广告媒体订单")
@RestController
@RequestMapping("/openapi/ad/pay")
@RequiredArgsConstructor
public class OpenApiAdOrderPayController {

    private final IAdPayOrderAppService adPayOrderAppService;

    private final IAdRefundOrderAppService adRefundOrderAppService;
    private final IUploadService uploadService;
    @Operation(summary = "广告信息")
    @GetMapping("/info")
    CommonResult<AdH5Resp> adInfo(@RequestParam("id") Long id) {
        return adPayOrderAppService.adInfo(id);
    }



    @Operation(summary = "提交下单")
    @PostMapping("/submit")
    CommonResult<AdPayOrderResp> submitOrder(@RequestBody AdSubmitOrderReq adSubmitOrderReq) {
        return adPayOrderAppService.submitOrder(adSubmitOrderReq);
    }

    @Operation(summary = "售后订单申请")
    @PostMapping("/refund/apply")
    CommonResult<Void> applyRefundOrder(@Validated @RequestBody AdOrderRefundApplyReq adOrderRefundApplyReq) {
        return adRefundOrderAppService.applyRefundOrder(adOrderRefundApplyReq);
    }
    @Operation(summary = "订单详情")
    @GetMapping("/detail")
    CommonResult<AdPayOrderResp> queryPayOrderDetail(@RequestParam("mchOrderNo") String mchOrderNo) {
        return adPayOrderAppService.queryDetail(mchOrderNo);
    }
    @PostMapping("/image")
    @Operation(summary ="上传图片")
    public CommonResult<String> image(HttpServletRequest request) {
        MultipartFile multipartFile;
        try {
            multipartFile = ((MultipartRequest) request).getFile("file");
        } catch (Exception e) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }

        if (multipartFile == null) {
            return CommonResult.error(CommonResultCode.FILE_NOT_EXIST);
        }
        return uploadService.upload(multipartFile, CommonEnum.OssEnum.IMAGE);
    }
}
